// sync.Cond 条件变量
package main

import (
	"log"
	"math/rand"
	"sync"
	"time"
)

func test1() {
	c := sync.NewCond(&sync.Mutex{})
	var readyCount int
	count := 5
	for i := 1; i <= count; i++ {
		go func(i int) {
			time.Sleep(time.Duration(rand.Int63n(10)) * time.Second) //随机等待时长,模拟不定时有人到场
			c.L.Lock()
			readyCount++
			c.L.Unlock()
			log.Printf("第 %d 个人已进入会场\n", i)
			c.Broadcast()
		}(i)
	}
	c.L.Lock()
	for readyCount != count {
		c.Wait()
		log.Println("主持人已登记")
	}
	c.L.Unlock()
	log.Println("所有人都已进入会场,会议开始")
}

func main() {
	test1()
}
